package com.umer.pinban.customer;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Rect;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.common.bean.InfoResult;
import com.common.bean.MsgBean;
import com.common.bean.UserInfo;
import com.common.okhttp.OkEntityListRequest;
import com.common.okhttp.OkSimpleRequest;
import com.common.ui.barlibrary.ImmersionBar;
import com.common.ui.recycleview.decoration.DividerGridItemDecoration;
import com.common.ui.recycleview.listener.OptListener;
import com.common.utils.Constants;
import com.common.utils.EventConstants;
import com.common.utils.screen.ScreenUtil;
import com.umer.pinban.AppHolder;
import com.umer.pinban.R;
import com.umer.pinban.adapter.customer.SexSelectAdapter;
import com.umer.pinban.adapter.data.DataDictionnaryAdapter;
import com.umer.pinban.adapter.staff.StaffSelectAdapter;
import com.umer.pinban.base.BasicActivity;
import com.umer.pinban.bean.customer.CustomerBean;
import com.umer.pinban.bean.customer.SexBean;
import com.umer.pinban.bean.data.DataDictionaryBean;
import com.umer.pinban.bean.staff.StaffBean;
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;

public class AddCustomerActivity extends BasicActivity implements OptListener, com.wdullaer.materialdatetimepicker.date.DatePickerDialog.OnDateSetListener {

    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.linear_submit)
    LinearLayout linearSubmit;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_staff)
    TextView tvStaff;
    @BindView(R.id.tv_from)
    TextView tvFrom;
    @BindView(R.id.tv_brithday)
    TextView tvBrithday;
    @BindView(R.id.et_address)
    EditText etAddress;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_grade)
    TextView tvGrade;
    @BindView(R.id.btn)
    Button btn;
    private String name = "";
    private String phone = "";
    private String address = "";
    private String referrerCustomerId = "";
    private int customerStatus = 1;//0无效  1有效
    private int type = 1;//1选择性别 2选择优美师 3选择来源 4类型 5等级
    private String sex = "";
    private UserInfo userInfo;

    @Override
    public boolean isImmersionBarEnabled() {
        return true;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                //是否可以修改导航栏颜色，默认为true
                .navigationBarEnable(true)
                //导航栏颜色，不写默认黑色
                .navigationBarColor(R.color.barcolor)
                .statusBarColor(R.color.colorPrimary)
                //解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                .fitsSystemWindows(true)
                .init();
    }

    @Override
    public View isSupportLoadSirEnabled() {
        return null;
    }

    @Override
    public boolean isSupportSwipeBackEnabled() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_add_customer;
    }

    @Override
    public void init() {
        userInfo = AppHolder.getInstance().getUserInfo();
        personnelId = super.getIntent().getIntExtra("personnelId",-1);
        personneName = super.getIntent().getStringExtra("personneName");
        if(!TextUtils.isEmpty(personneName)){
            tvStaff.setText(personneName);
        }
        simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
        initSexData();
        etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(!TextUtils.isEmpty(s.toString())){
                    name = s.toString();
                }
            }
        });
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(!TextUtils.isEmpty(s.toString())){
                    phone = s.toString();
                }
            }
        });
        etAddress.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(!TextUtils.isEmpty(s.toString())){
                    address = s.toString();
                }
            }
        });
    }

    @Override
    public void loadData() {

    }

    @OnClick({R.id.linear_back, R.id.tv_sex,R.id.tv_staff,R.id.tv_from,
            R.id.tv_brithday,R.id.tv_type,R.id.tv_grade,R.id.btn})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.btn:
                if(TextUtils.isEmpty(name)){
                    showToast("请输入会员姓名");
                }else if(TextUtils.isEmpty(sex)){
                    showToast("请选择会员性别");
                }else if(TextUtils.isEmpty(phone)){
                    showToast("请输入会员手机号码");
                }else if(personnelId == -1){
                    showToast("请选择会员所属优美师");
                }else if(TextUtils.isEmpty(birthday)){
                    showToast("请选择会员生日");
                }else if(TextUtils.isEmpty(address)){
                    showToast("请输入会员地址");
                }else {
                    showProgress("正在请求...");
                    OkSimpleRequest okSimpleRequest = new OkSimpleRequest(R.id.addCustomer,
                            Constants.addCustomer);
                    okSimpleRequest.addParams("shopId", userInfo.getShopId());
                    okSimpleRequest.addParams("personnelId", personnelId);
                    if(label.equals("转介绍")){
                        if(customerId!=-1){
                            okSimpleRequest.addParams("referrerCustomerId", customerId);
                        }
                    }else {
                        okSimpleRequest.addParams("customerSource", label);
                    }
                    okSimpleRequest.addParams("customerType", customerType);
                    okSimpleRequest.addParams("customerLevel", customerGrade);
                    okSimpleRequest.addParams("name", name);
                    okSimpleRequest.addParams("phone", phone);
                    okSimpleRequest.addParams("birthday", toData(birthday +"  00:00:00"));
                    okSimpleRequest.addParams("address", address);
                    okSimpleRequest.addParams("sex", sex);
                    okSimpleRequest.addParams("customerStatus", customerStatus);
                    okSimpleRequest.setHeader(true);
                    requestOkhttpSimple(okSimpleRequest);
                }
                break;
            case R.id.linear_back:
                finish();
                break;
            case R.id.tv_sex:
                type = 1;
                selectSex();
                break;
            case R.id.tv_staff:
                type = 2;
                if(dialogStaff == null){
                    selectStaffList();
                }else {
                    if(!dialogStaff.isShowing()){
                        dialogStaff.show();
                    }
                }
                break;
            case R.id.tv_from:
                type = 3;
                if(dialogFrom == null){
                    selectDataList();
                }else {
                    if(!dialogFrom.isShowing()){
                        dialogFrom.show();
                    }
                }
                break;
            case R.id.tv_type:
                type = 4;
                if(dialogType == null){
                    selectDataListType();
                }else {
                    if(!dialogType.isShowing()){
                        dialogType.show();
                    }
                }
                break;
            case R.id.tv_grade:
                type = 5;
                if(dialogGrade == null){
                    selectDataListGrade();
                }else {
                    if(!dialogGrade.isShowing()){
                        dialogGrade.show();
                    }
                }
                break;
            case R.id.tv_brithday:
                setDate();
                break;
            default:
                break;
        }
    }

    @Override
    public void onOptClick(View view, Object object) {
        int position = (int) object;
        switch (view.getId()){
            case R.id.tv:
                if(type==1){
                    if(sexSelectAdapter.getDataSource().get(position).isSelect()){
                        sexSelectAdapter.getDataSource().get(position).setSelect(false);
                        sex = "";
                    }else {
                        sexSelectAdapter.getDataSource().get(position).setSelect(true);
                        sex = sexSelectAdapter.getDataSource().get(position).getContent();
                    }
                    //单选
                    for(int i= 0;i<sexSelectAdapter.getDataSource().size();i++){
                        if(i!=position){
                            sexSelectAdapter.getDataSource().get(i).setSelect(false);
                        }
                    }
                    sexSelectAdapter.notifyDataSetChanged();
                }else if(type==2){

                    if(staffSelectAdapter.getDataSource().get(position).isSelect()){
                        staffSelectAdapter.getDataSource().get(position).setSelect(false);
                        personnelId = -1;
                        personneName = "";
                    }else {
                        staffSelectAdapter.getDataSource().get(position).setSelect(true);
                        personnelId = staffSelectAdapter.getDataSource().get(position).getPersonnelId();
                        personneName = staffSelectAdapter.getDataSource().get(position).getName();
                    }
                    //单选
                    for(int i= 0;i<staffSelectAdapter.getDataSource().size();i++){
                        if(i!=position){
                            staffSelectAdapter.getDataSource().get(i).setSelect(false);
                        }
                    }
                    staffSelectAdapter.notifyDataSetChanged();
                }
                //来源
                else if(type==3){
                    label = dataDictionnaryAdapter.getDataSource().get(position).getLabel();
                    if(!TextUtils.isEmpty(label) && label.equals("转介绍")){
                        Intent intent = new Intent(AddCustomerActivity.this,SelectCustomerActivity.class);
                        intent.putExtra("customerId",customerId);
                        startActivity(intent);
                    }else {
                        if(dataDictionnaryAdapter.getDataSource().get(position).isSelect()){
                            dataDictionnaryAdapter.getDataSource().get(position).setSelect(false);
                            label = "";
                        }else {
                            dataDictionnaryAdapter.getDataSource().get(position).setSelect(true);
                            label = dataDictionnaryAdapter.getDataSource().get(position).getLabel();
                        }
                        //单选
                        for(int i= 0;i<dataDictionnaryAdapter.getDataSource().size();i++){
                            if(dataDictionnaryAdapter.getDataSource().get(i).getLabel().equals("转介绍")){
                                customerId = -1;
                                customerName = "";
                            }
                            if(i!=position){
                                dataDictionnaryAdapter.getDataSource().get(i).setSelect(false);
                            }
                        }
                        dataDictionnaryAdapter.notifyDataSetChanged();
                    }
                }else if(type==4){
                    if(dataDictionnaryAdapterType.getDataSource().get(position).isSelect()){
                        dataDictionnaryAdapterType.getDataSource().get(position).setSelect(false);
                        customerType = "";
                    }else {
                        dataDictionnaryAdapterType.getDataSource().get(position).setSelect(true);
                        customerType = dataDictionnaryAdapterType.getDataSource().get(position).getLabel();
                    }
                    //单选
                    for(int i= 0;i<dataDictionnaryAdapterType.getDataSource().size();i++){
                        if(i!=position){
                            dataDictionnaryAdapterType.getDataSource().get(i).setSelect(false);
                        }
                    }
                    dataDictionnaryAdapterType.notifyDataSetChanged();
                }else if(type==5){
                    if(dataDictionnaryAdapterGrade.getDataSource().get(position).isSelect()){
                        dataDictionnaryAdapterGrade.getDataSource().get(position).setSelect(false);
                        customerGrade = "";
                    }else {
                        dataDictionnaryAdapterGrade.getDataSource().get(position).setSelect(true);
                        customerGrade = dataDictionnaryAdapterGrade.getDataSource().get(position).getLabel();
                    }
                    //单选
                    for(int i= 0;i<dataDictionnaryAdapterGrade.getDataSource().size();i++){
                        if(i!=position){
                            dataDictionnaryAdapterGrade.getDataSource().get(i).setSelect(false);
                        }
                    }
                    dataDictionnaryAdapterGrade.notifyDataSetChanged();
                }
                break;
            default:break;
        }

    }

    @Override
    public void handlerMeg(MsgBean msgBean) {
        super.handlerMeg(msgBean);
        int flag = msgBean.getFlag();
        if (flag == EventConstants.select_customer) {
            CustomerBean customer = (CustomerBean) msgBean.getObject();
            if (customer != null) {
                customerId = customer.getCustomerId();
                customerName = customer.getName();
                for(int i=0;i<dataDictionaryBeans.size();i++){
                    if(dataDictionaryBeans.get(i).getLabel().equals("转介绍")){
                        dataDictionaryBeans.get(i).setCustomerId(customerId);
                        dataDictionaryBeans.get(i).setCustomerName(customerName);
                        dataDictionaryBeans.get(i).setSelect(true);
                    }else {
                        dataDictionaryBeans.get(i).setSelect(false);
                    }
                }
            }else {
                customerId = -1;
                customerName = "";
                for(int i=0;i<dataDictionaryBeans.size();i++){
                    if(dataDictionaryBeans.get(i).getLabel().equals("转介绍")){
                        dataDictionaryBeans.get(i).setCustomerId(customerId);
                        dataDictionaryBeans.get(i).setCustomerName(customerName);
                        dataDictionaryBeans.get(i).setSelect(false);
                    }
                }
            }
            dataDictionnaryAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {
        if(what == R.id.addCustomer){
            hideProgress();
            showToast(t.getDesc());
            finish();
        }else if(what == R.id.personnelList){
            staffBeans = (List<StaffBean>) t.getT();
            if(personnelId != -1){
                if(staffBeans!=null && staffBeans.size()>0){
                    for(int i=0;i<staffBeans.size();i++){
                        if(staffBeans.get(i).getPersonnelId() == personnelId){
                            staffBeans.get(i).setSelect(true);
                        }
                    }
                }
            }
            selectStaff();
        }else if(what == R.id.dataDictionary){
            dataDictionaryBeans = (List<DataDictionaryBean>) t.getT();
            selectStaffFrom();
        }else if(what == R.id.dataDictionaryType){
            dataDictionaryBeansType = (List<DataDictionaryBean>) t.getT();
            selectStaffType();
        }else if(what == R.id.dataDictionaryGrade){
            dataDictionaryBeansGrade = (List<DataDictionaryBean>) t.getT();
            selectStaffGrade();
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {
        if(what == R.id.addCustomer){
            hideProgress();
            showToast(t.getDesc());
        }
    }

    private void selectStaffList() {
        OkEntityListRequest<StaffBean> request = new OkEntityListRequest<>(R.id.personnelList,
                Constants.personnelList, StaffBean.class);
        request.addParams("shopId", userInfo.getShopId());
        request.setHeader(true);
        requestOkhttpEntityList(request);
    }

    private void selectDataList() {
        OkEntityListRequest<DataDictionaryBean> request = new OkEntityListRequest<>(R.id.dataDictionary,
                Constants.dataDictionary, DataDictionaryBean.class);
        request.addParams("type", "CUSTOMER_SOURCE");
        request.setHeader(true);
        requestOkhttpEntityList(request);
    }

    private void selectDataListType() {
        OkEntityListRequest<DataDictionaryBean> request = new OkEntityListRequest<>(R.id.dataDictionaryType,
                Constants.dataDictionary, DataDictionaryBean.class);
        request.addParams("type", "CUSTOMER_TYPE");
        request.setHeader(true);
        requestOkhttpEntityList(request);
    }

    private void selectDataListGrade() {
        OkEntityListRequest<DataDictionaryBean> request = new OkEntityListRequest<>(R.id.dataDictionaryGrade,
                Constants.dataDictionary, DataDictionaryBean.class);
        request.addParams("type", "CUSTOMER_LEVEL");
        request.setHeader(true);
        requestOkhttpEntityList(request);
    }

    /**
     * 验证手机号码
     *
     * @param phone
     * @return
     */
    private boolean validatePhone(String phone) {
        if (TextUtils.isEmpty(phone)) {
            showToast("请输入手机号码");
            return false;
        }else if(phone.length()>11){
            showToast("请输入正确的手机号码");
        }
        return true;
    }

    //=====================================生日选择=============================================
    private String birthday;
    private SimpleDateFormat simpleDateFormat;

    private void setDate(){
        /*final Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog dialog = new DatePickerDialog(AddCustomerActivity.this,
                new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int selectedYear, int selectedMonth, int selectedDay) {
                calendar.set(Calendar.YEAR, selectedYear);
                calendar.set(Calendar.MONTH, selectedMonth);
                calendar.set(Calendar.DAY_OF_MONTH, selectedDay);
                birthday = simpleDateFormat.format(calendar.getTime());
                tvBrithday.setText(birthday);
            }
        }, year, month, day);
        dialog.show();*/
        Calendar now = Calendar.getInstance();
        DatePickerDialog dpd = DatePickerDialog.newInstance(
                AddCustomerActivity.this,
                now.get(Calendar.YEAR), // Initial year selection
                now.get(Calendar.MONTH), // Initial month selection
                now.get(Calendar.DAY_OF_MONTH) // Inital day selection
        );
        dpd.show(getSupportFragmentManager(), "Datepickerdialog");
    }

    @Override
    public void onDateSet(com.wdullaer.materialdatetimepicker.date.DatePickerDialog view, int year,
                          int monthOfYear, int dayOfMonth) {
        birthday = year + "-" +(monthOfYear+1) + "-" +dayOfMonth;
        tvBrithday.setText(birthday);
    }

    private String toData(String time){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//小写的mm表示的是分钟
        Date date= null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//小写的mm表示的是分钟
        String str=sdf2.format(date);
        return str;
    }

    //=====================================性别选择=============================================
    private List<SexBean> sexBeans = new ArrayList<>();
    private SexSelectAdapter sexSelectAdapter;

    private void initSexData() {
        SexBean sexBean1 = new SexBean();
        sexBean1.setContent("男");
        sexBeans.add(sexBean1);
        SexBean sexBean2 = new SexBean();
        sexBean2.setContent("女");
        sexBeans.add(sexBean2);
        sexSelectAdapter = new SexSelectAdapter(this,sexBeans,R.layout.item_dialog_one, this);
    }
    private RecyclerView recyclerSex;
    private Dialog dialogSex;
    private LinearLayout linearSex,linear_ok_sex;

    private void selectSex() {
        if (dialogSex == null) {
            dialogSex = new Dialog(this, R.style.ActionSheetDialogStyle);
            //填充对话框的布局
            View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_sex, null);
            linearSex = (LinearLayout) inflate.findViewById(R.id.linear_sex);
            linear_ok_sex= (LinearLayout) inflate.findViewById(R.id.linear_ok_sex);
            recyclerSex = (RecyclerView) inflate.findViewById(R.id.recycler_sex);
            recyclerSex.setLayoutManager(new GridLayoutManager(this, 9));
            recyclerSex.addItemDecoration(new DividerGridItemDecoration(this));
            recyclerSex.setAdapter(sexSelectAdapter);
            linearSex.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (dialogSex.isShowing()) {
                        dialogSex.dismiss();
                    }
                }
            });
            linear_ok_sex.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    tvSex.setText(sex);
                    dialogSex.dismiss();
                }
            });
            //将布局设置给Dialog
            dialogSex.setContentView(inflate);
            Window dialogWindow = dialogSex.getWindow();
            WindowManager.LayoutParams windowparams = dialogWindow.getAttributes();
            //设置Dialog从窗体底部弹出
            dialogWindow.setGravity(Gravity.BOTTOM);
            Rect rect = new Rect();
            View view1 = dialogWindow.getDecorView();
            view1.getWindowVisibleDisplayFrame(rect);
            windowparams.width = ScreenUtil.getInstance().getScreenWidth();
            dialogWindow.setAttributes(
                    (WindowManager.LayoutParams) windowparams);
            //获得窗体的属性
//            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//            lp.y = 10;//设置Dialog距离底部的距离
//            //将属性设置给窗体
//            dialogWindow.setAttributes(lp);
            dialogSex.show();
            dialogSex.setCanceledOnTouchOutside(false);
            dialogSex.setCancelable(false);
        } else {
            if (!dialogSex.isShowing()) {
                dialogSex.show();
            }
        }

    }

    //=====================================优美师选择=============================================
    private RecyclerView recyclerStaff;
    private Dialog dialogStaff;
    private LinearLayout linearStaff,linear_ok_staff;
    //美容师
    private List<StaffBean> staffBeans = new ArrayList<>();
    private StaffSelectAdapter staffSelectAdapter;
    private int personnelId = -1;
    private String personneName;

    private void selectStaff() {
        if (dialogStaff == null || staffBeans.size()==0) {
            dialogStaff = new Dialog(this, R.style.ActionSheetDialogStyle);
            //填充对话框的布局
            View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_staff, null);
            linearStaff = (LinearLayout) inflate.findViewById(R.id.linear_staff);
            linear_ok_staff = (LinearLayout) inflate.findViewById(R.id.linear_ok_staff);
            recyclerStaff = (RecyclerView) inflate.findViewById(R.id.recycler_staff);
            staffSelectAdapter = new StaffSelectAdapter(this,staffBeans,R.layout.item_dialog_one,this);
            recyclerStaff.setLayoutManager(new GridLayoutManager(this, 9));
            recyclerStaff.addItemDecoration(new DividerGridItemDecoration(this));
            recyclerStaff.setAdapter(staffSelectAdapter);
            linearStaff.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (dialogStaff.isShowing()) {
                        dialogStaff.dismiss();
                    }
                }
            });
            linear_ok_staff.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    tvStaff.setText(personneName);
                    dialogStaff.dismiss();
                }
            });
            //将布局设置给Dialog
            dialogStaff.setContentView(inflate);
            Window dialogWindow = dialogStaff.getWindow();
            WindowManager.LayoutParams windowparams = dialogWindow.getAttributes();
            //设置Dialog从窗体底部弹出
            dialogWindow.setGravity(Gravity.BOTTOM);
            Rect rect = new Rect();
            View view1 = dialogWindow.getDecorView();
            view1.getWindowVisibleDisplayFrame(rect);
            windowparams.width = ScreenUtil.getInstance().getScreenWidth();
            dialogWindow.setAttributes(
                    (WindowManager.LayoutParams) windowparams);
            //获得窗体的属性
//            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//            lp.y = 10;//设置Dialog距离底部的距离
//            //将属性设置给窗体
//            dialogWindow.setAttributes(lp);
            dialogStaff.show();
            dialogStaff.setCanceledOnTouchOutside(false);
            dialogStaff.setCancelable(false);
        } else {
            if (!dialogStaff.isShowing()) {
                dialogStaff.show();
            }
        }

    }

    //=====================================来源=============================================
    private RecyclerView recyclerFrom;
    private Dialog dialogFrom;
    private LinearLayout linearFrom,linear_ok_from;
    private List<DataDictionaryBean> dataDictionaryBeans = new ArrayList<>();
    private DataDictionnaryAdapter dataDictionnaryAdapter;
    private String label;
    private int customerId = -1;//转介绍
    private String customerName = "";

    private void selectStaffFrom() {
        if (dialogFrom == null || dataDictionaryBeans.size()==0) {
            dialogFrom = new Dialog(this, R.style.ActionSheetDialogStyle);
            //填充对话框的布局
            View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_staff_from, null);
            linearFrom = (LinearLayout) inflate.findViewById(R.id.linear_from);
            linear_ok_from = (LinearLayout) inflate.findViewById(R.id.linear_ok_from);
            recyclerFrom = (RecyclerView) inflate.findViewById(R.id.recycler_from);
            dataDictionnaryAdapter = new DataDictionnaryAdapter(this,dataDictionaryBeans,
                    R.layout.item_dialog_one,this);
            recyclerFrom.setLayoutManager(new GridLayoutManager(this, 9));
            recyclerFrom.addItemDecoration(new DividerGridItemDecoration(this));
            recyclerFrom.setAdapter(dataDictionnaryAdapter);
            linearFrom.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (dialogFrom.isShowing()) {
                        dialogFrom.dismiss();
                    }
                }
            });
            linear_ok_from.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(customerId!=-1){
                        tvFrom.setText(customerName);
                    }else {
                        tvFrom.setText(label);
                    }
                    dialogFrom.dismiss();
                }
            });
            //将布局设置给Dialog
            dialogFrom.setContentView(inflate);
            Window dialogWindow = dialogFrom.getWindow();
            WindowManager.LayoutParams windowparams = dialogWindow.getAttributes();
            //设置Dialog从窗体底部弹出
            dialogWindow.setGravity(Gravity.BOTTOM);
            Rect rect = new Rect();
            View view1 = dialogWindow.getDecorView();
            view1.getWindowVisibleDisplayFrame(rect);
            windowparams.width = ScreenUtil.getInstance().getScreenWidth();
            dialogWindow.setAttributes(
                    (WindowManager.LayoutParams) windowparams);
            //获得窗体的属性
//            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//            lp.y = 10;//设置Dialog距离底部的距离
//            //将属性设置给窗体
//            dialogWindow.setAttributes(lp);
            dialogFrom.show();
            dialogFrom.setCanceledOnTouchOutside(false);
            dialogFrom.setCancelable(false);
        } else {
            if (!dialogFrom.isShowing()) {
                dialogFrom.show();
            }
        }

    }

    //=====================================类型=============================================
    private RecyclerView recyclerType;
    private Dialog dialogType;
    private LinearLayout linearType,linear_ok_type;
    //美容师
    private List<DataDictionaryBean> dataDictionaryBeansType = new ArrayList<>();
    private DataDictionnaryAdapter dataDictionnaryAdapterType;
    private String customerType = "";

    private void selectStaffType() {
        if (dialogType == null || dataDictionaryBeansType.size()==0) {
            dialogType = new Dialog(this, R.style.ActionSheetDialogStyle);
            //填充对话框的布局
            View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_type, null);
            linearType = (LinearLayout) inflate.findViewById(R.id.linear_type);
            linear_ok_type = (LinearLayout) inflate.findViewById(R.id.linear_ok_type);
            recyclerType = (RecyclerView) inflate.findViewById(R.id.recycler_type);
            dataDictionnaryAdapterType = new DataDictionnaryAdapter(this,dataDictionaryBeansType,
                    R.layout.item_dialog_one,this);
            recyclerType.setLayoutManager(new GridLayoutManager(this, 9));
            recyclerType.addItemDecoration(new DividerGridItemDecoration(this));
            recyclerType.setAdapter(dataDictionnaryAdapterType);
            linearType.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (dialogType.isShowing()) {
                        dialogType.dismiss();
                    }
                }
            });
            linear_ok_type.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    tvType.setText(customerType);
                    dialogType.dismiss();
                }
            });
            //将布局设置给Dialog
            dialogType.setContentView(inflate);
            Window dialogWindow = dialogType.getWindow();
            WindowManager.LayoutParams windowparams = dialogWindow.getAttributes();
            //设置Dialog从窗体底部弹出
            dialogWindow.setGravity(Gravity.BOTTOM);
            Rect rect = new Rect();
            View view1 = dialogWindow.getDecorView();
            view1.getWindowVisibleDisplayFrame(rect);
            windowparams.width = ScreenUtil.getInstance().getScreenWidth();
            dialogWindow.setAttributes(
                    (WindowManager.LayoutParams) windowparams);
            //获得窗体的属性
//            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//            lp.y = 10;//设置Dialog距离底部的距离
//            //将属性设置给窗体
//            dialogWindow.setAttributes(lp);
            dialogType.show();
            dialogType.setCanceledOnTouchOutside(false);
            dialogType.setCancelable(false);
        } else {
            if (!dialogType.isShowing()) {
                dialogType.show();
            }
        }

    }

    //=====================================等级=============================================
    private RecyclerView recyclerGrade;
    private Dialog dialogGrade;
    private LinearLayout linearGrade,linear_ok_grade;
    //美容师
    private List<DataDictionaryBean> dataDictionaryBeansGrade = new ArrayList<>();
    private DataDictionnaryAdapter dataDictionnaryAdapterGrade;
    private String customerGrade="";

    private void selectStaffGrade() {
        if (dialogGrade == null || dataDictionaryBeansGrade.size()==0) {
            dialogGrade = new Dialog(this, R.style.ActionSheetDialogStyle);
            //填充对话框的布局
            View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_grade, null);
            linearGrade = (LinearLayout) inflate.findViewById(R.id.linear_grade);
            linear_ok_grade = (LinearLayout) inflate.findViewById(R.id.linear_ok_grade);
            recyclerGrade = (RecyclerView) inflate.findViewById(R.id.recycler_grade);
            dataDictionnaryAdapterGrade = new DataDictionnaryAdapter(this,dataDictionaryBeansGrade,
                    R.layout.item_dialog_one,this);
            recyclerGrade.setLayoutManager(new GridLayoutManager(this, 9));
            recyclerGrade.addItemDecoration(new DividerGridItemDecoration(this));
            recyclerGrade.setAdapter(dataDictionnaryAdapterGrade);
            linearGrade.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (dialogGrade.isShowing()) {
                        dialogGrade.dismiss();
                    }
                }
            });
            linear_ok_grade.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    tvGrade.setText(customerGrade);
                    dialogGrade.dismiss();
                }
            });
            //将布局设置给Dialog
            dialogGrade.setContentView(inflate);
            Window dialogWindow = dialogGrade.getWindow();
            WindowManager.LayoutParams windowparams = dialogWindow.getAttributes();
            //设置Dialog从窗体底部弹出
            dialogWindow.setGravity(Gravity.BOTTOM);
            Rect rect = new Rect();
            View view1 = dialogWindow.getDecorView();
            view1.getWindowVisibleDisplayFrame(rect);
            windowparams.width = ScreenUtil.getInstance().getScreenWidth();
            dialogWindow.setAttributes(
                    (WindowManager.LayoutParams) windowparams);
            //获得窗体的属性
//            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//            lp.y = 10;//设置Dialog距离底部的距离
//            //将属性设置给窗体
//            dialogWindow.setAttributes(lp);
            dialogGrade.setCanceledOnTouchOutside(false);
            dialogGrade.setCancelable(false);
            dialogGrade.show();
        } else {
            if (!dialogGrade.isShowing()) {
                dialogGrade.show();
            }
        }

    }
}
